package crm.view;

import crm.dao.impl.AdminServerImpl;
import crm.dao.impl.CustomerSqlSeverImpl;
import crm.dao.impl.EmployeeSqlSeverImpl;
import crm.entity.Admin;
import crm.entity.Customer;
import crm.entity.Employee;
import crm.view.main.AdminMain;
import crm.view.main.CustomerMain;
import crm.view.main.EmployeeMain;
import sun.java2d.Disposer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginPage implements ActionListener {
    JFrame frame = null;
    JPanel panel = null;
    JLabel type = null;
    JComboBox cmb = null;
    JLabel uLabel;
    JLabel pwdLabel;
    JTextField textField;
    JPasswordField pwdFiled;
    JButton btn;
    JButton btn2;

    public static void main(String[] args) {
        new LoginPage();

    }

    public LoginPage() {
        init();
        frame.setBounds(600, 200, 865, 600);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }

    public void init() {
        frame = new JFrame("登录页面");
        panel = new JPanel();    //创建面板
        frame.add(panel);        //面板添加到frame
        uLabel = new JLabel("用户名：");
        pwdLabel = new JLabel("密码：");
        btn = new JButton("登录");
        btn2 = new JButton("注册");
        textField = new JTextField();
        pwdFiled = new JPasswordField();

        Font font1 = new Font("宋体", Font.BOLD, 20);

        // 用户名和密码
        uLabel.setBounds(300, 110, 120, 40);
        panel.add(uLabel);
        pwdLabel.setBounds(300, 180, 120, 40);
        panel.add(pwdLabel);
        textField.setBounds(400, 110, 170, 40);
        panel.add(textField);
        pwdFiled.setBounds(400, 180, 170, 40);
        panel.add(pwdFiled);
        btn.setBounds(360, 320, 80, 40);
        btn2.setBounds(520, 320, 80, 40);


        panel.add(btn2);
        panel.add(btn);
        //  下拉框
        type = new JLabel("用户类型：");    //创建标签
        cmb = new JComboBox();    //创建JComboBox
        cmb.addItem("--请选择--");    //向下拉列表中添加一项
        cmb.addItem("--管理员--");
        cmb.addItem("--客户--");
        cmb.addItem("--职员--");
        panel.add(type);
        panel.add(cmb);

        type.setBounds(300, 240, 150, 60);
        cmb.setBounds(400, 240, 170, 40);
        panel.setLayout(null);

        uLabel.setFont(font1);
        pwdFiled.setFont(font1);
        pwdLabel.setFont(font1);
        textField.setFont(font1);
        btn.setFont(font1);
        btn2.setFont(font1);
        type.setFont(font1);
        cmb.setFont(font1);

        //加载图片
        ImageIcon icon = new ImageIcon("src/img/送货.png");
        //Image im=new Image(icon);
        //将图片放入label中
        JLabel imgLabel = new JLabel(icon);
        //设置label的大小
        imgLabel.setBounds(0, 50, icon.getIconWidth(), icon.getIconHeight());
        panel.add(imgLabel);

//        收货.png
        //加载图片
        ImageIcon icon2 = new ImageIcon("src/img/收货.png");
        //Image im=new Image(icon);
        //将图片放入label中
        JLabel imgLabel2 = new JLabel(icon2);
        //设置label的大小
        imgLabel2.setBounds(600, 50, icon2.getIconWidth(), icon2.getIconHeight());
        panel.add(imgLabel2);

        JLabel label = new JLabel("CRM客户管理系统");
        label.setFont(new Font("Dialog", 1, 35));
        label.setBounds(300, 0, 350, 100);
        panel.add(label);
        btn.addActionListener(this);
        btn2.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        String sss = cmb.getSelectedItem().toString();
        if ("".equals(textField.getText()) && "".equals(pwdFiled.getText())) {
            JOptionPane.showMessageDialog(null, "用户名、密码不能为空！");
        }
        //  管理员登录管理 admin
        else if (sss.equals("--管理员--")) {
            AdminServerImpl adminDao = new AdminServerImpl();
            Admin admin = new Admin();

            if (e.getSource() == btn) {
                String name = textField.getText();
                String pwd = pwdFiled.getText();
                admin.setName(name);
                admin.setPassword(pwd);
                if (adminDao.findAdmin(admin)) {
                    AdminMain adminMain = new AdminMain();
                    new Thread(adminMain).start();
                    frame.dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "尊敬的管理员，请您输入正确的用户名、密码！");
                }
            } else if (e.getSource() == btn2) {
                String name = textField.getText();
                String pwd = pwdFiled.getText();
                admin.setName(name);
                admin.setPassword(pwd);
                JOptionPane.showMessageDialog(null, "注册成功！");
                int i = adminDao.add(admin);
                if (i > 0) {
                    System.out.println("注册成功！");
                }
            }

            //  客户登录管理  customer
        } else if (sss.equals("--客户--")) {
            Customer customer = new Customer();
            CustomerSqlSeverImpl customerDao = new CustomerSqlSeverImpl();
            if (e.getSource() == btn) {
                String name = textField.getText();
                String pwd = pwdFiled.getText();

                customer.setCustomer_name(name);
                customer.setCustomer_pwd(pwd);

                if (customerDao.findCum(customer)) {
                    CustomerMain customerMain = new CustomerMain();
                    new Thread(customerMain).start();
                    frame.dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "尊敬的客户，请您输入正确的用户名、密码！");
                }
            } else if (e.getSource() == btn2) {
                String name = textField.getText();
                String pwd = pwdFiled.getText();
                customer.setCustomer_name(name);
                customer.setCustomer_pwd(pwd);
                JOptionPane.showMessageDialog(null, "注册成功！");
                int i = customerDao.add(customer);
                if (i > 0) {
                    System.out.println("注册成功！");
                }
            }


            //  职员登录管理  employee
        } else if (sss.equals("--职员--")) {
            EmployeeSqlSeverImpl employeeDao = new EmployeeSqlSeverImpl();
            Employee employee = new Employee();

            if (e.getSource() == btn) {
                String name = textField.getText();
                String pwd = pwdFiled.getText();
                employee.setEmployee_name(name);
                employee.setEmployee_pwd(pwd);
                if (employeeDao.findEmp(employee)) {
                    EmployeeMain employeeMain = new EmployeeMain();
                    new Thread(employeeMain).start();
                    frame.dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "尊敬的职员，请您输入正确的用户名、密码！");
                }
            } else if (e.getSource() == btn2) {
                String name = textField.getText();
                String pwd = pwdFiled.getText();
                employee.setEmployee_name(name);
                employee.setEmployee_pwd(pwd);
                JOptionPane.showMessageDialog(null, "注册成功！");
                int i = employeeDao.add(employee);
                if (i > 0) {
                    System.out.println("注册成功！");
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "尊敬的用户，请选择您的身份！");
        }


    }


}